package bsp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.io.Writable;
import org.apache.hama.bsp.BSPPeer;
import org.apache.hama.bsp.sync.SyncException;

import util.Pair;

public abstract class CMapper<IK, IV, OK, OV, MS extends Writable> {

	protected static final String TOKEN = " ";
	List<Pair<String, MS>> messageBox;
	protected int myId;
	protected String myName;

	protected int numPeers;

	public CMapper() {
		this.messageBox = new ArrayList<Pair<String, MS>>();
	}

	// public abstract void map(Context<IK, IV, OK, OV, MS> context)throws
	// IOException, SyncException, InterruptedException;

	public void addMesg(String dist, MS msg) {

		messageBox.add(new Pair<String, MS>(dist, msg));
	}

	public abstract void map(BSPPeer<IK, IV, OK, OV, MS> context)
			throws IOException, SyncException, InterruptedException;
	
	public void sendAllMesgs(BSPPeer<IK, IV, OK, OV, MS> peer)
			throws IOException {
		for (Pair<String, MS> msg : messageBox) {
			peer.send(msg.getFirst(), msg.getSecond());
		}
	}

	public void setup(BSPPeer<IK, IV, OK, OV, MS> context) throws IOException,
			SyncException, InterruptedException {
		myName = context.getPeerName();
		myId = context.getPeerIndex();
		numPeers = context.getNumPeers();

	}

	public void setup(BSPPeer<IK, IV, OK, OV, MS> context,
			CMapper<IK, IV, OK, OV, MS> mapper) throws IOException,
			SyncException, InterruptedException {
		//TODO:???????????????????????
		setup(context);

	}

}
